package com.housekeeper.accessory.service.impl;

import com.housekeeper.accessory.mapper.AccessoryDetailMapper;
import com.housekeeper.accessory.mapper.AccessoryMapper;
import com.housekeeper.accessory.mapper.AccessoryOwnerMapper;
import com.housekeeper.accessory.mapper.AccessorySupplierMapper;
import com.housekeeper.accessory.pojos.Accessory;
import com.housekeeper.accessory.pojos.AccessoryOrder;
import com.housekeeper.accessory.pojos.AccessoryOwner;
import com.housekeeper.accessory.pojos.AccessorySupplier;
import com.housekeeper.api.accessory.service.BillAccessoryService;
import com.housekeeper.api.manage.dtos.bill.OrderDetailDto;
import com.housekeeper.common.exception.HKException;
import com.housekeeper.common.response.ResponseEnum;
import com.housekeeper.common.utils.BeanHelper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Author:王子
 * 配件账单业务
 */
@Service
public class BillAccessoryServiceImpl implements BillAccessoryService {
    @Autowired
    private AccessoryMapper accessoryMapper;
    @Autowired
    private AccessorySupplierMapper accessorySupplierMapper;
    @Autowired
    private AccessoryDetailMapper accessoryDetailMapper;
    @Autowired
    private AccessoryOwnerMapper ownerMapper;

    @Override
    public OrderDetailDto getOrderById(Long orderCode) {
        if (orderCode == null){
            throw new HKException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        AccessoryOrder accessoryOrder = accessoryMapper.findByCode(orderCode);
        Long supCode = accessorySupplierMapper.getSupCodeById(accessoryOrder.getSupId());
        String describe = accessoryDetailMapper.getDescribeById(accessoryOrder.getAccId());
        OrderDetailDto orderDetailDto = BeanHelper.copyProperties(accessoryOrder, OrderDetailDto.class);
        orderDetailDto.setRCode(supCode);
        Long rOwnerId = ownerMapper.findOwnerIdBySupId(accessoryOrder.getSupId());
        orderDetailDto.setROwnerId(rOwnerId);
        orderDetailDto.setOrderId(accessoryOrder.getId());
        orderDetailDto.setDescribe(describe);
        orderDetailDto.setSum(accessoryOrder.getSum());
        orderDetailDto.setOrderCode(accessoryOrder.getOrderCode());
        return orderDetailDto;
    }
}
